<?php
    include(dirname(__FILE__)."/../../../Dao/db.php");

    function getAllMedia()
    {
        //メンバId・媒体種別ごとのすべてのキャンペーン
        $query = "SELECT id,name FROM listingmst";
        $result = mysql_query($query);
        while($row=mysql_fetch_array($result))
        {
            $option .= "<option value=\"{$row['id']}\">{$row['name']}</option>";
        }
        return $option;
    }

    function getAllCampaigns()
    {
        //メンバIdを取得
        session_start();
        $memberId = $_SESSION['memberId'];

        //媒体種別を取得
        if(!isset($_POST['search_media_select']))
        {
            $listingType = 1;
        }
        else
        {
            $listingType = $_POST['search_media_select'];
        }
        //メンバId・媒体種別ごとのすべてのキャンペーン
        $query = "SELECT camp_Id,name FROM campaign WHERE ";
        $query .= "memberId=$memberId AND ";
        $query .= "listingType=$listingType";

        $result = mysql_query($query);
        while($row=mysql_fetch_array($result))
        {
            $option .= "<option value=\"{$row['camp_Id']}\">{$row['name']}</option>\r\n";
        }
        return $option;
    }

    function searchAdGroups()
    {
        //メンバIdを取得
        session_start();
        $memberId = $_SESSION['memberId'];

        $listingType = $_POST['search_media_select'];
        $camp_Id = $_POST['search_campaign_select'];
        $adGr_Id = $_POST['search_id'];
        $name = $_POST['search_name'];
        $status = $_POST['search_status'];
        $sync = $_POST['search_sync'];
        $matchType = $_POST['search_type'];

        //広告グループ検索
        $query = "SELECT b.name campaignName,a.adGr_Id,";
        $query .= "a.name,a.landingUrl,a.maxCpc,a.maxContentCpc,a.status,a.sync ";
        $query .= "FROM adgroup a JOIN campaign b ";
        $query .= "ON a.camp_Id=b.camp_Id ";
        $query .= "WHERE ";
        $query .= "a.memberId=$memberId AND ";
        $query .= "a.listingType=$listingType AND ";
        //キャンペーンは「指定なし」以外
        if($camp_Id != "-1")
        {
            $query .= "a.camp_Id=$camp_Id AND ";
        }
        if(isset($adGr_Id) && $adGr_Id != "")
        {
            $query .= "a.adGr_Id=$adGr_Id AND ";
        }
        if(isset($name) && $name != "")
        {
            $query .= "a.name".getMatchCondition($name,$matchType)." AND ";
        }
        if($status != "-1")
        {
            $query .= "a.status=$status AND ";
        }
        else
        {
            $query .= "(a.status=0 OR a.status=1) AND ";
        }

        if($sync != "0")
        {
            $query .= "a.sync=$sync";
        }
        else
        {
            $query .= "(a.sync=1 OR a.sync=2 OR a.sync=3)";
        }

        $result = mysql_query($query);
        $count = 0;
        while($row=mysql_fetch_array($result))
        {
            $count++;

            $trs .= "<tr class=\"tr4\">
                        <td align=\"center\">{$row['adGr_Id']}</td>";
            if($row['sync'] == 2)
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_updating.gif\" border=\"0\" title=\"更新待ち\" /></td>";
            }
            else if($row['sync'] == 1)
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_on.gif\" border=\"0\" title=\"更新済み\" /></td>";
            }
            else
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_error.gif\" border=\"0\" title=\"更新エラー\" /></td>";
            }
			

            $trs .="<td align=\"left\">{$row['campaignName']}</td>
                    <td align=\"left\">{$row['name']}</td>
                    <td align=\"left\">{$row['landingUrl']}</td>
                    <td align=\"center\">{$row['maxCpc']}</td>
                    <td align=\"center\">{$row['maxContentCpc']}</td>
                    <td align=\"center\">".exchange_status($row['status'])."</td>
                </tr>";
        }
        $_POST['count'] = $count;
        return $trs;
    }

    function searchCreatives()
    {
        //メンバIdを取得
        session_start();
        $memberId = $_SESSION['memberId'];

        $listingType = $_POST['search_media_select'];
        $camp_Id = $_POST['search_campaign_select'];
        $Crt_Id = $_POST['search_id'];
        $name = $_POST['search_name'];
        $status = $_POST['search_status'];
        $sync = $_POST['search_sync'];
        $matchType = $_POST['search_type'];

        //クリエイティブ検索
        $query = "SELECT c.name campaignName,a.Crt_Id,b.name adGroupName,a.name creativeName,";
        $query .= "a.headLine,a.description1,a.description2,";
        $query .= "a.displayUrl,a.destinationUrl,a.status,a.sync ";
        $query .= "FROM creative a JOIN adgroup b ON a.adGr_Id=b.adGr_Id ";
        $query .= "JOIN campaign c ON a.camp_Id=c.camp_Id ";
        $query .= "WHERE ";
        $query .= "a.memberId=$memberId AND ";
        $query .= "a.listingType=$listingType AND ";
        //キャンペーンは「指定なし」以外
        if($camp_Id != "-1")
        {
            $query .= "a.camp_Id=$camp_Id AND ";
        }
        if(isset($Crt_Id) && $Crt_Id != "")
        {
            $query .= "a.Crt_Id=$Crt_Id AND ";
        }
        if(isset($name) && $name != "")
        {
            $query .= "a.name".getMatchCondition($name,$matchType)." AND ";
        }
        if($status != "-1")
        {
            $query .= "a.status=$status AND ";
        }
        else
        {
            $query .= "(a.status=0 OR a.status=1) AND ";
        }

        if($sync != "0")
        {
            $query .= "a.sync=$sync";
        }
        else
        {
            $query .= "(a.sync=1 OR a.sync=2 OR a.sync=3)";
        }

        $result = mysql_query($query);
        $count = 0;
        while($row=mysql_fetch_array($result))
        {
            $count++;
            $trs .= "<tr class=\"tr3\">
                        <td align=\"center\">{$row['Crt_Id']}</td>";
            if($row['sync'] == 2)
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_updating.gif\" border=\"0\" title=\"更新待ち\" /></td>";
            }
            else if($row['sync'] == 1)
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_on.gif\" border=\"0\" title=\"更新済み\" /></td>";
            }
            else
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_error.gif\" border=\"0\" title=\"更新エラー\" /></td>";
            }
            $trs .="<td align=\"left\">{$row['campaignName']}</td>
                    <td align=\"left\">{$row['adGroupName']}</td>
                    <td align=\"left\">{$row['creativeName']}</td>
                                    <td align=\"left\"><div class=\"creative_preview\">
                                            <div class=\"creative_preview_title\">{$row['headLine']}</div>
                                            <div class=\"creative_preview_description\">{$row['description1']}<br />
            {$row['description2']}</div>
                                            <div class=\"creative_preview_url\">{$row['displayUrl']}</div>
                                    </div></td>
                                    <td align=\"left\">{$row['destinationUrl']}</td>
                                    <td align=\"center\">".exchange_status($row['status'])."</td>
                                </tr>";
        }
        $_POST['count'] = $count;
        return $trs;
    }

    function searchKeywords()
    {
        //メンバIdを取得
        session_start();
        $memberId = $_SESSION['memberId'];

        $listingType = $_POST['search_media_select'];
        $camp_Id = $_POST['search_campaign_select'];
        $key_Id = $_POST['search_id'];
        $text = $_POST['search_name'];
        $status = $_POST['search_status'];
        $sync = $_POST['search_sync'];
        $matchType = $_POST['search_type'];

        //キーワード検索
        $query = "SELECT c.name campaignName,a.key_Id,b.name adGroupName,a.text,";
        $query .= "a.type,a.maxCpc,a.destinationUrl,a.paused,a.sync ";
        $query .= "FROM keyword a JOIN adgroup b ON a.adGr_Id=b.adGr_Id ";
        $query .= "JOIN campaign c ON a.camp_Id=c.camp_Id ";
        $query .= "WHERE ";
        $query .= "a.memberId=$memberId AND ";
        $query .= "a.listingType=$listingType AND ";
        //キャンペーンは「指定なし」以外
        if($camp_Id != "-1")
        {
            $query .= "a.camp_Id=$camp_Id AND ";
        }
        if(isset($key_Id) && $key_Id != "")
        {
            $query .= "a.key_Id=$key_Id AND ";
        }
        if(isset($text) && $text != "")
        {
            $query .= "a.text".getMatchCondition($text,$matchType)." AND ";
        }
        if($status != "-1")
        {
            $query .= "a.paused=(1-$status) AND a.status!=9 AND ";
        }
        if($sync != "0")
        {
            $query .= "a.sync=$sync";
        }
        else
        {
            $query .= "(a.sync=1 OR a.sync=2 OR a.sync=3)";
        }

        $result = mysql_query($query);
        $count = 0;
        while($row=mysql_fetch_array($result))
        {
            $count++;
            if($row['type'] == 2)
            {
                $type = "部分一致";
            }
            else
            {
                $type = "全部一致";
            }

            if($row['paused'] == 1)
            {
                $status = "Off";
            }
            else
            {
                $status = "On";
            }

            $trs .= "<tr class=\"tr4\">
                        <td align=\"center\">{$row['key_Id']}</td>";
            if($row['sync'] == 2)
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_updating.gif\" border=\"0\" title=\"更新待ち\" /></td>";
            }
            else if($row['sync'] == 1)
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_on.gif\" border=\"0\" title=\"更新済み\" /></td>";
            }
            else
            {
                $trs .= "<td align=\"center\"><img src=\"../images/sync_error.gif\" border=\"0\" title=\"更新エラー\" /></td>";
            }
            $trs .="<td align=\"left\">{$row['campaignName']}</td>
                                    <td align=\"left\">{$row['adGroupName']}</td>
                                    <td align=\"left\">{$row['text']}</td>
                                    <td align=\"center\">$type</td>
                                    <td align=\"center\">{$row['maxCpc']}</td>
                                    <td align=\"left\">{$row['destinationUrl']}</td>
                                    <td align=\"center\">$status</td>
                                </tr>";
        }
        $_POST['count'] = $count;
        return $trs;
    }

    function getMatchCondition($name,$type)
    {
        if($type == 0)
        {
            return "='$name'";
        }
        else
        {
            return " like binary '%$name%'";
        }
    }

    function exchange_status($status)
    {
        if($status == 0)
        {
            return "Off";
        }
        else if($status == 1)
        {
            return "On";
        }
    }
?>
